package com.iqiyi.pps.epg.core.service.program;

import com.iqiyi.pps.epg.api.model.web.program.TProgramDirectorCommentary;
import com.iqiyi.pps.epg.api.model.web.program.TProgramDirectorResource;
import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.program.ProgramDirectorResourceDao;
import com.iqiyi.pps.epg.core.model.program.ProgramDirectorResource;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangjing on 2017/3/13.
 */
@Service
@Transactional
public class ProgramDirectorResourceServiceImpl implements IProgramDirectorResourceService {

    private Logger logger = LoggerFactory.getLogger(ProgramDirectorResourceServiceImpl.class);

    @Autowired
    private ProgramDirectorResourceDao dao;

    @Override
    public ProgramDirectorResource get(int id) {
        return dao.get(id);
    }

    @Override
    public List<TProgramDirectorResource> getList(TFilter filter) {
        List<TProgramDirectorResource> result = new ArrayList<TProgramDirectorResource>();
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        List<ProgramDirectorResource> list = dao.find(filterList);
        TProgramDirectorResource thriftEntity = null;
        for (ProgramDirectorResource entity : list) {
            thriftEntity = entity.toThriftEntity();
            result.add(thriftEntity);
        }
        return result;
    }

    @Override
    public List<ProgramDirectorResource> findList(TFilter filter) {
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        return  dao.find(filterList);
    }

    @Override
    public ProgramDirectorResource add(ProgramDirectorResource entity) {
        dao.saveOnly(entity);
        return entity;
    }

    @Override
    public void update(ProgramDirectorResource entity) {
        dao.save(entity);
    }

    @Override
    public List<ProgramDirectorResource> findByDefault(int directorId) {
        Criteria criteria = dao.createCriteria();
        criteria.add(Restrictions.eq("directorId", directorId));
        criteria.add(Restrictions.ne("isDeleted", 1));
        criteria.add(Restrictions.eq("isBackup", 1));
        criteria.add(Restrictions.eq("isDefaultBackup", 1));
        List<ProgramDirectorResource> list = dao.find(criteria);
        return list;
    }


    @Override
    public void cleanResourceByDirectorId(int directorId) {
        String hql = "update ProgramDirectorResource set isDeleted=1 where directorId = ? and isDeleted = 0 ";
        int executeUpdateNum =  dao.createQuery(hql).setInteger(0,directorId).executeUpdate();
        logger.info("[cleanResourceByDirectorId][directorId={}][executeUpdateNum={}]",directorId,executeUpdateNum);
    }

    @Override
    public List<TProgramDirectorCommentary> getCommentaryList(TFilter filter) {
        List<TProgramDirectorCommentary> result = new ArrayList<>();
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        List<ProgramDirectorResource> list = dao.find(filterList);

        TProgramDirectorCommentary thriftEntity = null;
        for (ProgramDirectorResource entity : list) {
            thriftEntity = transferResourceToCommentary(entity);
            result.add(thriftEntity);
        }
        return result;
    }

    private TProgramDirectorCommentary transferResourceToCommentary(ProgramDirectorResource entity) {
        TProgramDirectorCommentary result = new TProgramDirectorCommentary();

        try {
            result.setId(entity.getId());
            result.setDirectorId(entity.getDirectorId());
            result.setProgramQipuId(entity.getProgramQipuId());
            result.setName(entity.getSignalName());
            result.setStreamUrl(entity.getSignalUrl());
            result.setStatus(entity.getStatus());
            result.setVolumn(Integer.valueOf(entity.getVolumn()));
        } catch (Exception e) {
            logger.info("[transferResourceToCommentary] e= " + e);
        }

        return result;
    }

    @Override
    public int deleteCommentaryAllByDirectorId(int directorId) {
        String hql = "UPDATE ProgramDirectorResource set status = -1 where directorId = ? and status != -1 and resourceType = 2 ";
        int executeUpdateNum =  dao.createQuery(hql).setInteger(0,directorId).executeUpdate();
        logger.info("[deleteCommentaryAllByDirectorId][directorId={}][executeUpdateNum={}]",directorId,executeUpdateNum);

        return executeUpdateNum;
    }
}
